package com.huarui.sale.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.ewaf.framework.action.BaseAction;

import com.huarui.part.model.Part;
import com.huarui.part.service.PartService;
import com.huarui.sale.model.Express;
import com.huarui.sale.model.Sale;
import com.huarui.sale.model.SaleItem;
import com.huarui.sale.service.SaleService;
import com.huarui.user.model.SessionMessage;
import com.huarui.user.security.UserHolder;

/**
 * 销售单增加处理器
 * 
 * @author xu
 * 
 */
public class SaleAddAction extends BaseAction {

	private static final long serialVersionUID = -3873362464055217530L;

	private SaleService saleService;

	private PartService partService;

	private Map<String, Object> jsonObject;

	private SessionMessage sessionMessage;

	private String queryParam;

	/**
	 * 保存成草稿
	 * 
	 * @return
	 */
	public String saveSale() {

		sessionMessage = UserHolder.getCurrentSessionMessage();
		
		Integer companyId = sessionMessage.getCompanyId();
		Integer userId=sessionMessage.getUserId();

		JSONObject queryObj = JSONObject.fromObject(queryParam);

		Sale sale = (Sale) JSONObject.toBean(queryObj.getJSONObject("sale"),
				Sale.class);

		sale.setSellerId(companyId);
		sale.setCreatorId(userId);

		JSONArray jaSaleItem = queryObj.getJSONArray("saleItem");
		for (int i = 0; i < jaSaleItem.size(); i++) {
			SaleItem saleItem = (SaleItem) JSONObject.toBean(jaSaleItem
					.getJSONObject(i), SaleItem.class);

			Part part = partService.getPartById(saleItem.getPartId());

			if (part == null) {

				jsonObject.put("success", false);
				return JSON;

			} else {
				saleItem.setCode(part.getCode());
				saleItem.setName(part.getName());
				saleItem.setNameEn(part.getNameEn());
				saleItem.setPyCode(part.getPyCode());
				saleItem.setSource(part.getSource());
				saleItem.setPartCategoryId(part.getPartCategoryId());
				saleItem.setMeasurementUnit(part.getMeasurementUnit());
				saleItem.setLevel1Name(part.getLevel1Name());
				saleItem.setLevel2Name(part.getLevel2Name());
				saleItem.setLevel1Id(part.getLevel1Id());
				//sale.getSaleItem().add(saleItem);
			}

		}

		List<Express> expressList = new ArrayList<Express>();

		//saleService.saleSave(sale, expressList);

		jsonObject = new HashMap<String, Object>();
		jsonObject.put("success", true);
		//List<Handler> list = saleService.getButtonsByStatus(SaleButtonTypeEnum.STATUS_OF_NEWCREATE.getType(), 0, 0);
		//jsonObject.put("buttons", list);
		
		return JSON;
	}

	public void setSaleService(SaleService saleService) {
		this.saleService = saleService;
	}

	public void setPartService(PartService partService) {
		this.partService = partService;
	}

	public Map<String, Object> getJsonObject() {
		return jsonObject;
	}

	public void setJsonObject(Map<String, Object> jsonObject) {
		this.jsonObject = jsonObject;
	}

	public String getQueryParam() {
		return queryParam;
	}

	public void setQueryParam(String queryParam) {
		this.queryParam = queryParam;
	}

}
